package net.sk.china.city.service.impl;

import net.sk.china.city.utils.DateUtils;
import net.sk.china.city.utils.SecurityUtils;
import net.sk.china.common.exception.ServiceException;
import net.sk.china.city.utils.PlatformIdHolder;
import net.sk.china.city.domain.ArHomeProduct;
import net.sk.china.city.domain.vo.AddHomeProductVO;
import net.sk.china.city.domain.vo.HomeProductInfoVO;
import net.sk.china.city.domain.vo.UpdateHomeProductVO;
import net.sk.china.city.mapper.ArHomeProductMapper;
import net.sk.china.city.service.IArHomeProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

/**
 * 商品管理Service业务层处理
 * 
 * @author gxf
 * @date 2024-03-29
 */
@Service
public class ArHomeProductServiceImpl implements IArHomeProductService
{
    @Autowired
    private ArHomeProductMapper arHomeProductMapper;
    /**
     * 查询商品管理
     * 
     * @param id 商品管理主键
     * @return 商品管理
     */
    @Override
    public HomeProductInfoVO selectArHomeProductById(Long id)
    {
        return arHomeProductMapper.selectArHomeProductById(id);
    }

    /**
     * 查询商品管理列表
     * 
     * @param arHomeProduct 商品管理
     * @return 商品管理
     */
    @Override
    public List<HomeProductInfoVO> selectArHomeProductList(ArHomeProduct arHomeProduct)
    {
        arHomeProduct.setPlatformId(PlatformIdHolder.getPlatformId());
        return arHomeProductMapper.selectArHomeProductList(arHomeProduct);
    }

    /**
     * 新增商品管理
     *
     * @return 结果
     */
    @Override
    @Transactional
    public int insertArHomeProduct(AddHomeProductVO addHomeProductVO)
    {
        ArHomeProduct arHomeProduct = new ArHomeProduct();
        arHomeProduct.setProductName(addHomeProductVO.getProductName());
        arHomeProduct.setIndexImg(addHomeProductVO.getIndexImg());
        arHomeProduct.setProductStatus(addHomeProductVO.getProductStatus());
        arHomeProduct.setCategoryId(addHomeProductVO.getCategoryId());
        arHomeProduct.setSalesVolume(0);
        arHomeProduct.setPlatformId(PlatformIdHolder.getPlatformId());
        arHomeProduct.setMonovalent(addHomeProductVO.getMonovalent());
        arHomeProduct.setInventory(addHomeProductVO.getInventory());
        arHomeProduct.setProCode(addHomeProductVO.getProCode());
        arHomeProduct.setProDetails(addHomeProductVO.getProDetails());
        arHomeProduct.setCostPrice(addHomeProductVO.getCostPrice());
        arHomeProduct.setShopId(addHomeProductVO.getShopId());
        arHomeProduct.setSupplierId(addHomeProductVO.getSupplierId());
        arHomeProduct.setCreateTime(LocalDate.now());
        arHomeProduct.setCreateBy(SecurityUtils.getUsername());
        int i1 = arHomeProductMapper.insertArHomeProduct(arHomeProduct);
        if(i1 <=0 ){
            throw new ServiceException("新增失败",409);
        }
        return 1;
    }

    /**
     * 修改商品管理
     *
     * @return 结果
     */
    @Override
    @Transactional
    public int updateArHomeProduct(UpdateHomeProductVO updateHomeProductVO)
    {
        ArHomeProduct arHomeProduct = new ArHomeProduct();
        arHomeProduct.setId(updateHomeProductVO.getId());
        arHomeProduct.setProductName(updateHomeProductVO.getProductName());
        arHomeProduct.setIndexImg(updateHomeProductVO.getIndexImg());
        arHomeProduct.setProductStatus(updateHomeProductVO.getProductStatus());
        arHomeProduct.setCategoryId(updateHomeProductVO.getCategoryId());
        arHomeProduct.setSupplierId(updateHomeProductVO.getSupplierId());
        arHomeProduct.setMonovalent(updateHomeProductVO.getMonovalent());
        arHomeProduct.setInventory(updateHomeProductVO.getInventory());
        arHomeProduct.setProCode(updateHomeProductVO.getProCode());
        arHomeProduct.setCostPrice(updateHomeProductVO.getCostPrice());
        arHomeProduct.setProDetails(updateHomeProductVO.getProDetails());
        arHomeProduct.setUpdateTime(LocalDate.now());
        arHomeProduct.setUpdateBy(SecurityUtils.getUsername());
        int i1 = arHomeProductMapper.updateById(arHomeProduct);
        if(i1 <=0 ){
            throw new ServiceException("修改失败",409);
        }
        return 1;
    }

    /**
     * 批量删除商品管理
     * 
     * @param ids 需要删除的商品管理主键
     * @return 结果
     */
    @Override
    public int deleteArHomeProductByIds(Long[] ids)
    {
//        return arHomeProductMapper.deleteArHomeProductByIds(ids);
        for (Long id : ids){
            ArHomeProduct arHomeProduct = new ArHomeProduct();
            arHomeProduct.setId(id);
            arHomeProduct.setDelFlag("2");
            int i1 = arHomeProductMapper.updateById(arHomeProduct);
            if(i1 <=0 ){
                throw new ServiceException("删除失败",409);
            }
        }
        return 1;
    }

    /**
     * 删除商品管理信息
     * 
     * @param id 商品管理主键
     * @return 结果
     */
    @Override
    public int deleteArHomeProductById(Long id)
    {
        ArHomeProduct arHomeProduct = new ArHomeProduct();
        arHomeProduct.setId(id);
        arHomeProduct.setDelFlag("2");
        int i1 = arHomeProductMapper.updateById(arHomeProduct);
        if(i1 <=0 ){
            throw new ServiceException("删除失败",409);
        }
        return 1;
    }
}
